<?php
class Video extends MY_Admin_Controller
{
    public function index()
    {
        $page = intval($this->input->get_post('page'));
        $business = intval($this->input->get_post('business'));
        
        $limit = 8;
    	$page = $page ? $page : 1;
        $offset = ($page - 1) * $limit;


        $status = $this->input->get_post('status');
        ('' == $status) && $status = AUDIT_STATUS_NOT;


        $where = array
        (
            'type'     => AUDIT_TYPE_VIDEO,
            'business' => $business,
            'status'   => $status,
        );
        
        $this->load->model('common/Model_audit_content', 'ac');
        $total = $this->ac->count($where);
        $data  = $this->ac->lists($offset, $limit, $where);
        
        
        //分页
        $this->load->library('pagination');
        
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        $config['base_url'] = '/audit/Video/index';
        
        $this->pagination->initialize($config);
        $page_html = $this->pagination->create_links();


        if (AUDIT_STATUS_YES == $status && $data)
        {
            $cids = array_column($data, 'cid');

            $this->load->model('common/Model_audit_log', 'al');
            $log_data = $this->al->getByIn('cid', $cids);

            foreach ($log_data as $ldv)
            {
                $data[$ldv['cid']]['log'][] = $ldv;
            }
        }


        $data = array
        (
            'data'      => $data,
            'page_html' => $page_html,
            'status'    => $status,
        	'business'  => $business,
            'total'     => $total,
        );


        $this->load->view('admin/audit/video/index', $data);
    }


    /**
     * 审核通过
     */
    public function pass_in()
    {
        $response = array('code' => '200', 'msg' => '审核成功');

        do {
            $cid = $this->input->get_post('cid');


            if (empty($cid))
            {
                $response = error(100003);
                break;
            }
            
            if (!is_array($cid))
            {
            	$cid = array($cid);
            }

            $this->load->model('common/Model_audit_content', 'ac');
            
            foreach($cid as $v)
            {
            	$audit_rs = $this->ac->audit($v, AUDIT_OPERA_TYPE_PASS_IN);
            }
            
            if (200 != $audit_rs['status'])
            {
                $response = error($audit_rs['status']);
                break;
            }

        } while (false);


        $this->json_output($response);
    }


    /**
     * 删除
     */
    public function delete()
    {
        $response = array('code' => '200', 'msg' => '审核成功');


        do {
            $business = $this->input->get_post('business');
            $cid = $this->input->get_post('cid');


            if (empty($cid))
            {
                $response = error(100003);
                break;
            }


            if (!is_array($cid))
            {
                $cid = array($cid);
            }


            $this->load->model('common/Model_audit_content', 'ac');
            
            foreach($cid as $v)
            {
            	$audit_rs = $this->ac->audit($v, AUDIT_OPERA_TYPE_DELETE);
            }
            
            if (200 != $audit_rs['status'])
            {
            	$response = error($audit_rs['status']);
            	continue;
            }
            else
            {
            	$this->load->service('common/service_common_audit');
            	$delete_rs = $this->service_common_audit->delete($business, $cid);
            	
            	if(200 != $delete_rs['status'])
            	{
            		$response = error($delete_rs['status']);
            	}
            }    

        } while (false);


        $this->json_output($response);
    }


    /**
     * 恢复
     */
    public function restore()
    {
        $response = array('code' => '200', 'msg' => '审核成功');


        do {
        	$business = $this->input->get_post('business');
            $cid = $this->input->get_post('cid');


            if (empty($cid))
            {
                $response = error(100003);
                break;
            }


            if (!is_array($cid))
            {
                $cid = array($cid);
            }


            $this->load->model('common/Model_audit_content', 'ac');
            
            foreach($cid as $v)
            {
            	$audit_rs = $this->ac->audit($v, AUDIT_OPERA_TYPE_RESTORE);
            }
            
        	if (200 != $audit_rs['status'])
            {
            	$response = error($audit_rs['status']);
            	continue;
            }
            else
            {
            	$this->load->service('common/service_common_audit');
            	$restore_rs = $this->service_common_audit->restore($business, $cid);
            	
            	if(200 != $restore_rs['status'])
            	{
            		$response = error($restore_rs['status']);
            	}
            }

        } while (false);


        $this->json_output($response);
    }
}